<?php
namespace Member\Controller;
use \Member\Controller\InitController;
class SpaceController extends InitController{
	public function _initialize(){
		parent::_initialize();
		$this->sucai_db = D('Sucai');
		$this->favorite_db = D('MemberFavorite');
		$this->log_db = D('MemberLog');
		$this->download_db = D('Download');
	}
	/**
	 * 空间首页
	 */
	public function index(){
		$rs = $this->space_db->where(array('userid'=>$this->userid ))->getField('userid');
		if(!$rs){
			$this->error('您的空间没有开通',U('space'));
		}
		//我的作品
		$lists = $this->sucai_db->where(array('sysadd'=>0,'username'=>$this->userid,'catid'=>12))->order('inputtime DESC')->limit(8)->select();
		//近三个月更新作品
		$lists_time = $this->sucai_db->where(array('sysadd'=>0,'username'=>$this->userid,'catid'=>12,'inputtime'=>array('gt','DATE_SUB( CURRENT_DATE()  ,  INTERVAL  3  MONTH )')))->order('inputtime DESC')->limit(8)->select();
		/*出售作品*/
		
		//在素材表中查询该用户添加的作品
		$addproducts = $this->sucai_db->where(array('username'=>$this->userid))->select();
		$arr = array();
		foreach ($addproducts as $key=>$val) {
			$arr[] = $val['id'];
		}
		//在下载表中查询是否有作品的记录
		$downloads = $this->download_db->where(array('document_id'=>array('in',$arr)))->order('dateline DESC')->limit(8)->select();
		foreach ($downloads as $key=>$val) {
			$downloads[$key]['title'] = $this->sucai_db->getFieldById($val['document_id'],'title');
			$downloads[$key]['thumb'] = $this->sucai_db->getFieldById($val['document_id'],'thumb');
			$downloads[$key]['url'] = $this->sucai_db->getFieldById($val['document_id'],'url');
		}
		include template('member_space');
	}
	/**
	 * 开通空间
	 */
	public function space(){
		if(submitcheck('dosubmit')){
			$info = I('info');
			$info['userid'] = $this->userid;
			$info['inputtime'] = NOW_TIME;
			$info['spaceid'] = (int) I('spaceid');
			$result = $this->space_db->update($info);
			if(!$result){
				$this->error('开通空间失败');
			}
			$this->redirect('index');
		}else{
			$rs = $this->space_db->where(array('userid'=>$this->userid ))->getField('userid');
			if($rs){
				$result = $this->space_db->where(array('userid'=>$this->userid ))->find();
			}
			include template('space');
		}
	}
	/*作品*/
	public function works(){
		$pagecurr = max(1,I('page',0,'intval'));
		$pagesize = 32;
		$sqlMap = array();
		$sqlMap['sysadd'] = 0;
		$sqlMap['username'] = $this->userid;
		$sqlMap['catid'] = 12;
		$cout =  $this->sucai_db->where($sqlMap)->count();
		$lists = $this->sucai_db->where($sqlMap)->page($pagecurr,$pagesize)->order('inputtime DESC')->select();
		$pages = page($count,$pagesize);
		include template('member_space');
	}
	/*日志*/
	public function log(){
		$pagecurr = max(1,I('page',0,'intval'));
		$pagesize = 4;
		$sqlMap = array();
		$sqlMap['userid'] = $this->userid;
		$log_count = $this->log_db->where($sqlMap)->count();
		$log_lists = $this->log_db->where($sqlMap)->page($pagecurr,$pagesize)->order('inputtime DESC')->select();
		$pages = page($log_count,$pagesize);
		include template('member_space');
	}
	/*日志添加*/
	public function addlog(){
		if(submitcheck('dosubmit')){
			$info = I('log');
			$info['userid'] = $this->userid;
			$info['inputtime'] = NOW_TIME;
			$info['content'] = htmlspecialchars_decode($info['content']);
			$result = $this->log_db->add($info);
			if(!$result){
				$this->error($this->log_db->getError());
			}
			$this->success('操作成功');
		}else{
			$form = new \Common\Library\form();
			$show_validator = $show_dialog = 1;
			include template('member_log');
		}
	}
	/*日志查看*/
	public function show(){
		$logid = I('id');
		addhit($logid);
		$info = $this->log_db->where(array('logid'=>$logid))->find();
		//上一篇
		$info_prev =  $this->log_db->where(array('logid'=>array('lt',$logid)))->order('logid DESC')->find();
		//下一篇
		$info_next =  $this->log_db->where(array('logid'=>array('gt',$logid)))->order('logid ASC')->find();
		include template('member_log_show');
	}
	/*收藏*/
	public function collect(){
		$pagecurr = max(1,I('page',0,'intval'));
		$pagesize = 20;
		$sqlMap = array();
		$sqlMap['userid'] = $this->userid;
		//该用户收藏的素材
		$count = $this->favorite_db->where($sqlMap)->count();
		$lists = $this->favorite_db->where($sqlMap)->page($pagecurr,$pagesize)->select();
		//根据userid 获取素材的id
		foreach($lists as $key=>$val){
			$lists[$key]['thumb'] = $this->sucai_db->getFieldById($val['document_id'],'thumb');
		}
		$pages = page($count,$pagesize);
		include template('member_space');
	}
}